import {
  Body,
  Controller, Delete,
  Get,
  Param, ParseUUIDPipe,
  Patch,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
  ArticleIndexNormalDto,
  ArticleCreateDto,
  ArticleMineDto,
  ArticleUpdateDto,
} from './article.dto';
import { ArticleService } from './article.service';
import { getPager } from "@/common/pager-response";
import { User } from "@/common/auth/user.decorator";
import { UserModel } from "@/module/user/user.model";
import { UserAuthGuard } from "@/common/auth/user-auth.guard";
import {
  ArticleCreateResult,
  ArticleIndexMineResult,
  ArticleIndexNormalResult, ArticleShowMineResult,
  ArticleShowNormalResult,
} from '@/module/article/article.result';
import { PageResult } from "common/interface";
import { ApiPaginatedOkResponse } from "@/common/pager-response/api-paginated-ok-response.decorator";

@Controller('/v1/articles')
@ApiTags('Article')
export class ArticleV1Controller {
  constructor(private readonly articleService: ArticleService) {
  }

  @Get('/normal')
  @ApiOperation({ summary: '普通文章列表查询' })
  @ApiPaginatedOkResponse(ArticleIndexNormalResult)
  async indexNormal(@Query() query: ArticleIndexNormalDto): Promise<PageResult<ArticleIndexNormalResult>> {
    const res = await this.articleService.indexNormal(query);
    return getPager(res[0],res[1]);
  }

  @Get('/mine')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '我的文章列表' })
  @ApiPaginatedOkResponse(ArticleIndexMineResult)
  async indexMine(
    @Query() query: ArticleMineDto,
    @User() user: UserModel,
  ): Promise<PageResult<ArticleIndexMineResult>> {
    const res = await this.articleService.indexMine(query, user);
    return getPager(res[0], res[1]);
  }

  @Get('/mine/:uuid')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '我的文章详情' })
  @ApiOkResponse({ type: ArticleShowMineResult })
  async showMine(@Param('uuid', ParseUUIDPipe) uuid: string, @User() user: UserModel): Promise<ArticleShowMineResult | null> {
    const res = await this.articleService.showMine(uuid, user);
    return res;
  }

  @Post('/mine')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '新增我的文章' })
  @ApiOkResponse({ type: ArticleCreateResult })
  async createMine(
    @Body() body: ArticleCreateDto,
    @User() user: UserModel,
  ): Promise<ArticleCreateResult> {
    const res = await this.articleService.createMine(body, user);
    return res;
  }

  @Patch('/mine/:uuid')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '修改我的文章' })
  async updateMine(
    @Body() body: ArticleUpdateDto,
    @Param('uuid', ParseUUIDPipe) uuid: string,
    @User() user: UserModel,
  ): Promise<void> {
    await this.articleService.updateMine(body, uuid, user);
    return;
  }

  @Delete('/mine/:uuid')
  @UseGuards(UserAuthGuard)
  @ApiOperation({ summary: '删除我的文章' })
  async destroyMine(@Param('uuid', ParseUUIDPipe) uuid: string, @User() user: UserModel): Promise<void> {
    await this.articleService.destroyMine(uuid, user);
    return;
  }

  @Get('/normal/:uuid')
  @ApiOperation({ summary: '普通文章详情' })
  @ApiOkResponse({ type: ArticleShowNormalResult })
  async showNormal(@Param('uuid', ParseUUIDPipe) uuid: string): Promise<ArticleShowNormalResult | null> {
    const res = await this.articleService.showNormalArticle(uuid);
    return res;
  }

  @Get('/:uuid/recommend')
  @ApiOperation({ summary: '普通文章详情' })
  @ApiOkResponse({ type: ArticleIndexNormalResult, isArray: true })
  async recommend(@Param('uuid', ParseUUIDPipe) uuid: string): Promise<ArticleIndexNormalResult[]> {
    const res = await this.articleService.recommend(uuid);
    return res;
  }
}
